﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ExamRepositoryWPF.Models;

namespace ExamRepositoryWPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            foreach(string type in Properties.Settings.Default["ExamTypes"] as System.Collections.Specialized.StringCollection)
            {
                ComboBoxTypes.Items.Add(type);
            }

            List<object> questions = new List<object>()
            {
                new EaseAnswerQuestion()
                {
                    QuestionID = 1,
                    Level = 0,
                    LastModified = DateTime.Now,
                    Content = "1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?1+1=?",
                    Answer = "22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"

                },
                new EaseAnswerQuestion()
                {
                    QuestionID = 2,
                    Level = 1,
                    LastModified = DateTime.Now,
                    Content = "3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?3+3=?",
                    Answer = "666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666"
                },
                new JudgementQuestion()
                {
                    QuestionID = 3,
                    Level = 1,
                    LastModified = DateTime.Now,
                    Content = "5 + 5 = 10",
                    Answer = true
                },
                new SingleChoiceQuestion()
                {
                    QuestionID = 4,
                    Level = 1,
                    LastModified = DateTime.Now,
                    Content = "2 * 2",
                    SectionsObject = new List<string>(){"1","2","3","4"},
                    Answer = 3
                },
                new MultiChoiceQuestion()
                {
                    QuestionID = 4,
                    Level = 1,
                    LastModified = DateTime.Now,
                    Content = "猫可以做什么",
                    SectionsObject = new List<string>(){"吃饭","睡觉","打豆豆","螺旋升天"},
                    Answer = new List<int>(){0, 1}
                }
            };
            questionList.ItemsSource = questions;

            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:5000/singlechoicequestion/getalllist");
            //request.Method = "GET";
            //request.ContentType = "application/json";

            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //Stream myResponseStream = response.GetResponseStream();
            //StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            //string retString = myStreamReader.ReadToEnd();
            //myStreamReader.Close();
            //myResponseStream.Close();
            //List<SingleChoiceQuestion> questions = JsonConvert.DeserializeObject<List<SingleChoiceQuestion>>(retString);
            //questionList.ItemsSource = questions;
        }

        private void ButtonSplitter_Click(object sender, RoutedEventArgs e)
        {
            GridLength w = gridMain.ColumnDefinitions[2].Width;
            gridMain.ColumnDefinitions[2].Width = new GridLength(w.Value == 0 ? 2 : 0, GridUnitType.Star);
        }

        private void questionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            questionDetails.DataContext = null;
            questionDetails.Content = new Converters.QuestionTypeToTemplateConverter().Convert(questionList.SelectedItem, typeof(UserControl), null, System.Globalization.CultureInfo.CurrentCulture);
            questionDetails.DataContext = questionList.SelectedItem;
        }
    }
}
